<template>
  <el-card shadow="always">
    <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="category1Id" @change="category1IdChange">
          <el-option v-for="item in categoryList1" :label="item.name" :value="item.id" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="category2Id" @change="category2IdChange">
          <el-option v-for="item in categoryList2" :label="item.name" :value="item.id" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="category3Id">
          <el-option v-for="item in categoryList3" :label="item.name" :value="item.id" :key="item.id"/>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import {defineComponent} from 'vue';

export default defineComponent({
  name: 'CategoryList',
});
</script>
<script setup lang="ts">
import {storeToRefs} from 'pinia';
import {onMounted} from 'vue';

//引入小菠萝中的attr数据
import useAttrStore from '@/stores/attr';
//获取store的实例化对象
const attrStore = useAttrStore();
defineProps<{disabled:boolean}>()
//解构数据
const {
  categoryList1,
  category1Id,
  categoryList2,
  category2Id,
  category3Id,
  categoryList3,
} = storeToRefs(attrStore);

/*

// 通过watch的方式来监听id的变化获取下级列表;
//监听category1Id的变化动态获取categoryList2的值
watch(category1Id, () => {
  categoryList2.value = [];
  category2Id.value = null;
  category3Id.value = null;
  categoryList3.value = [];
  attrStore.getCategory2(category1Id.value);
});
//监听category2Id的变化申请获取categoryList3的值
watch(category2Id, () => {
  // console.log('category2被选择了');
  attrStore.getCategory3(category2Id.value);
});

*/
//设置change事件监听选项框的改变
const category1IdChange = () => {
  categoryList2.value = [];
  category2Id.value = null;
  category3Id.value = null;
  categoryList3.value = [];
  attrStore.getCategory2();
}
const category2IdChange = () => {
  category3Id.value = null;
  categoryList3.value = [];
  attrStore.getCategory3();
}

//初始化数据
onMounted(() => {
  attrStore.getCategory1();
});
</script>

<style scoped>

</style>